package com.ehotting.eaf.netty.code;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;


public class ByteToCommandEncoder extends MessageToByteEncoder<String> {
	
	private final Logger logger = LoggerFactory.getLogger(ByteToCommandEncoder.class);
	
	@Override
	protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
		byte[] b = msg.getBytes(StandardCharsets.UTF_8);
		out.writeBytes(new byte[2]);
		out.writeInt(b.length);
		out.writeBytes(b);
	}
}
